package com.example.composetutorial

import android.os.Bundle
import android.os.Looper
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview

class ListComposableActivity : ComponentActivity() {

  var myList=listOf<String>("dwdfwf","fwfwfw","fwfwftgedgw")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {

                //ListWithBug(myList)
            ListComposable(myList)
            println(Thread.activeCount())
            println(Thread.currentThread().name)
        }
    }
}

@Composable
fun ThreadComposable(){

    Thread {
        Looper.prepare()

            Button(onClick = { /*TODO*/ }) {
        }
        Looper.loop()
    }.start()
}

@Composable
@Deprecated("Example with bug")
fun ListWithBug(myList: List<String>) {
    var items = 0

    Row(horizontalArrangement = Arrangement.SpaceBetween) {
        Column {
            for (item in myList) {
                Text("Item: $item")
                items++ // Avoid! Side-effect of the column recomposing.
                //接着循环还是重组
            }
        }
        Text("Count: $items")

    }
}

@Composable
fun ListComposable(myList: List<String>) {
    Row(horizontalArrangement = Arrangement.SpaceBetween) {
        Column {
            for (item in myList) {
                Text("Item: $item")
            }
        }
        Text("Count: ${myList.size}")


}
}
@Composable
@Preview
fun PreviewListWithBug() {
    var myList= listOf<String>("dwdfwf","fwfwfw","fwfwfw")
    ListWithBug(myList)
}
